package com.saturn.titan.web.controller.baseinfo;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.saturn.titan.core.bean.PlatformRole;
import com.saturn.titan.core.bean.PlatformRoleTreeData;
import com.saturn.titan.core.comm.CRMCache;
import com.saturn.titan.core.comm.ResponseBean;
import com.saturn.titan.core.service.RoleService;
import com.saturn.titan.web.utils.CRMUtil;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) throws ParseException {
		return "baseinfo/role/edit";
	}
	
	@RequestMapping(value = "delete",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean delete(@RequestParam(value="id")Long id,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			roleService.deleteNode(id);
			rb.setResult(ResponseBean.RESULT_OK);
		} catch (Exception e) {
			e.printStackTrace();
			rb.setResult(ResponseBean.RESULT_NG);
			rb.setMeg(e.getMessage());
		}
		return rb;
	}
	
	@RequestMapping(value = "add",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean add(@Valid
			PlatformRoleTreeData role,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			PlatformRole a = new PlatformRole();
			a.setRoleNo(role.getRoleNo());
			a.setRoleName(role.getRoleName());
			a.setOrgId(CRMUtil.getCurrentUserOrgId());
			a.setDescription(role.getDescription());
			
			a.setCrtDate(new Date());
			a.setCrtOpr(CRMUtil.getCurrentUser().id);
			roleService.add(a);
			rb.setResult(ResponseBean.RESULT_OK);
			rb.setData(a);
		} catch (Exception e) {
			e.printStackTrace();
			rb.setResult(ResponseBean.RESULT_NG);
			rb.setMeg(e.getMessage());
		}
		return rb;
	}
	
	@RequestMapping(value = "update",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean update(@Valid
			PlatformRoleTreeData role,Model model) {
		ResponseBean rb = new ResponseBean();
		try {
			PlatformRole a = new PlatformRole();
			a.setId(role.getId());
			a.setRoleNo(role.getRoleNo());
			a.setRoleName(role.getRoleName());
			a.setDescription(role.getDescription());
			a.setVersion(role.getVersion());
			a.setLstEdtDate(new Date());
			a.setLstEdtOpr(CRMUtil.getCurrentUser().id);
			
			roleService.update(a);
			rb.setResult(ResponseBean.RESULT_OK);
			rb.setData(a);
		} catch (Exception e) {
			e.printStackTrace();
			Throwable tmp = CRMUtil.getDataHaveChangedException(e);
			if(tmp != null){
				rb.setMeg(tmp.getMessage());
			}else{
				rb.setMeg(e.getMessage());
			}
			rb.setResult(ResponseBean.RESULT_NG);
		}
		return rb;
	}
	
	@RequestMapping(value = "select")
	@ResponseBody
	public Map<String,Object> select(@RequestParam(value="roleName") String roleName) throws ParseException {
		
		PlatformRole ba = new PlatformRole();
		if(!CRMUtil.blankStr(roleName)){
			ba.setRoleName(roleName);
		}
		
		List<PlatformRole> baseRoles = roleService.findRole(ba);
		List<PlatformRoleTreeData> result = Lists.newArrayList();
		PlatformRoleTreeData tmp = null;
		for(PlatformRole role : baseRoles){
			tmp = new PlatformRoleTreeData();
			tmp.setId(role.getId());
			tmp.setRoleName(role.getRoleName());
			tmp.setRoleNo(role.getRoleNo());
			tmp.setDescription(role.getDescription());
			tmp.setCrtOpr(role.getCrtOpr());
			tmp.setCrtDate(role.getCrtDate());
			tmp.setLstEdtDate(role.getLstEdtDate());
			tmp.setLstEdtOpr(role.getLstEdtOpr());
			tmp.setIconCls("icon-group_key");
			tmp.setVersion(role.getVersion());
			tmp.setDisplayCrtOpr(CRMCache.displayOpr(role.getCrtOpr()));
			tmp.setDisplayLstEdtOpr(CRMCache.displayOpr(role.getLstEdtOpr()));
			result.add(tmp);
		}
		
		Map<String,Object> r = Maps.newHashMap();
		r.put("total", result.size());
		r.put("rows", result);
		r.put("foot", "");
		return r;
	}
	
	@RequestMapping(value = "selectRoleSelectOption")
	@ResponseBody
	public Map<String,Object> selectRoleSelectOption() throws ParseException {
		Map<String,Object> r = Maps.newHashMap();
		List<PlatformRole> result = roleService.findAllRole();
		r.put("total", result.size());
		r.put("rows", result);
		return r;
	}
	
}
